package com.epichust.bootumpp.service.impl;

import com.epichust.bootumpp.model.bo.FiltersBO;
import com.epichust.bootumpp.model.bo.Umpp5PlanOrderPublishBO;
import com.epichust.bootumpp.model.bo.Umpp5TaskOrderBO;
import com.epichust.bootumpp.model.dao.Umpp5ProOpCodeDao;
import com.epichust.bootumpp.model.dao.Umpp5TaskOrderDao;
import com.epichust.bootumpp.model.entity.Umpp5ProOpCode;
import com.epichust.bootumpp.service.IUmpp5ProOpCodeService;
import com.epichust.bootumpp.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：longyang
 * @date ：Created in 2021/1/7 14:05
 * @description：产品工序编码service
 */
@Service
@Transactional
public class Umpp5ProOpCodeServiceImpl implements IUmpp5ProOpCodeService {

    @Autowired
    private Umpp5ProOpCodeDao umpp5ProOpCodeDao;

    @Autowired
    private Umpp5TaskOrderDao umpp5TaskOrderDao;
    /**
     * 分页查询产品工序编码
     * @param map
     * @return
     */
    @Override
    public List<Umpp5ProOpCode> getAllUmpp5ProOpCode(Map<String, String> map) {
        List<Umpp5ProOpCode> list = umpp5ProOpCodeDao.getAllUmpp5ProOpCode(map);
        return list;
    }

    /**
     * 根据订单号查询所有工序信息
     * @param map
     * @return
     */
    @Override
    public List<Umpp5PlanOrderPublishBO> getUmpp5PlanOrderWithOp(Map<String, Object> map) {
        if(map.get("orderCode") != null){
            String [] orderCodeArr = map.get("orderCode").toString().split(",");
            map.put("orderCode",orderCodeArr);
        }
        //处理分页信息数据
        if(map.get("rows") != null && map.get("page") != null){
            int rows = Integer.parseInt(map.get("rows").toString());
            int page = Integer.parseInt(map.get("page").toString());
            int firstLimit = (page-1)*rows;
            int lastLimit = page*rows;
            map.put("firstLimit",firstLimit);
            map.put("lastLimit",lastLimit);
        }
        //处理前端查询条件
        if(map.get("filters") != null){
            String filtersStr = map.get("filters").toString();
            List<FiltersBO> filters = GsonUtils.parseString2List(filtersStr,FiltersBO.class);
            for(FiltersBO bo :filters){
                if("plannedBeginDate".equals(bo.getField()) && "ge".equals(bo.getCompare())){//计划开始时间大于
                    map.put("plannedBeginDateGe",bo.getData()+" 00:00:00");
                }else if("plannedBeginDate".equals(bo.getField()) && "le".equals(bo.getCompare())){//计划开始时间小于
                    map.put("plannedBeginDateLe",bo.getData()+" 23:59:59");
                }else if("plannedEndDate".equals(bo.getField()) && "ge".equals(bo.getCompare())){
                    map.put("plannedEndDateGe",bo.getData() + " 00:00:00");
                }else if("plannedEndDate".equals(bo.getField()) && "le".equals(bo.getCompare())){
                    map.put("plannedEndDateLe",bo.getData() + " 23:59:59");
                }else{
                    map.put(bo.getField(),bo.getData());
                }
            }
        }
        //查出订单下所有工序的信息
        List<Umpp5PlanOrderPublishBO> list = umpp5ProOpCodeDao.getUmpp5PlanOrderWithOp(map);

        for(Umpp5PlanOrderPublishBO bo : list){
            Map<String,String> boMap = new HashMap<String, String>();
            boMap.put("orderCode",bo.getCode());
            boMap.put("opCode",bo.getOpCode());
            List<Umpp5TaskOrderBO> tlist = umpp5TaskOrderDao.getAllUmpp5TaskOrder(boMap);
            BigDecimal sum = BigDecimal.ZERO;
            //累加相同订单下，相同工序的所有计划数量
            for(Umpp5TaskOrderBO tbo : tlist){
                sum = sum.add(tbo.getQty());
            }
            bo.setRemainQty(bo.getQty().subtract(sum));
        }
        String records = umpp5ProOpCodeDao.getUmpp5PlanOrderWithOpRecords(map);
        if(!list.isEmpty()){
            list.get(0).setRecords(records);
        }
        return list;
    }
}
